package yt.demo.appcommontools.utils.files;


import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import yt.demo.appcommonbean.exception.LogicException;
import yt.demo.appcommontools.utils.files.storage.AliyunStorageService;
import yt.demo.appcommontools.utils.files.storage.FileStorageService;
import yt.demo.appcommontools.utils.files.storage.LocalStorageService;

@Component
@Data
public class FileStorageServiceFactory {

    private final FileStorageProperties properties;
    private final LocalStorageService LocalStorageService;
    private final AliyunStorageService aliyunStorageService;

    @Autowired
    public FileStorageServiceFactory(FileStorageProperties properties, LocalStorageService LocalStorageService, AliyunStorageService aliyunStorageService) {
        this.properties = properties;
        this.LocalStorageService = LocalStorageService;
        this.aliyunStorageService = aliyunStorageService;
    }

    public FileStorageService createFileStorageService() {
        String defaultType = properties.getDefaultType();
        if ("local".equalsIgnoreCase(defaultType)) {
            return LocalStorageService;
        } else if ("aliyun".equalsIgnoreCase(defaultType)) {
            return aliyunStorageService;
        }
        throw new LogicException("Unsupported storage type: " + defaultType);
    }

    public String getDomainName() {
        FileStorageService service = createFileStorageService();
        return service.getDomainName();
    }
}
